﻿namespace CTP.CRM.Core.ExRedis
{
    public class LockControl : IDisposable
    {
        private ExRedisClient _client;

        private string _lockKey;

        private int _timeoutSeconds;

        private bool _isGetLock;

        public bool IsGetLock => _isGetLock;

        public LockControl(ExRedisClient client, string lockKey, int timeoutSeconds, bool isWait = false)
        {
            _client = client;
            _lockKey = lockKey;
            _timeoutSeconds = timeoutSeconds;
            if (isWait)
            {
                _isGetLock = client.WaitLock(_lockKey, _timeoutSeconds);
            }
            else
            {
                _isGetLock = client.Lock(_lockKey, _timeoutSeconds);
            }
        }

        public void Dispose()
        {
            _client.Unlock(_lockKey);
        }
    }
}
